package com.qctpos.crm.webapi.controller;

import com.qctpos.crm.common.annotation.PassToken;
import com.qctpos.crm.common.annotation.ResponseResult;
import com.qctpos.crm.common.utils.ExceptionUtil;
import com.qctpos.crm.domain.dto.ExSqlDto;
import com.qctpos.crm.domain.dto.UserLoginDto;
import com.qctpos.crm.domain.dto.UserLoginResultDto;
import com.qctpos.crm.service.Demo1Service;
import com.qctpos.crm.webapi.config.RabbitConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@Api(tags = "登录api")
@PassToken
@Slf4j
public class LoginController {

    @Autowired
    private Demo1Service baseService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 交换空间名称
     */
    @Value("${properties.queue.logbackexchange}")
    public String logbackexchange;

    /**
     * 设置路由key
     */
    @Value("${properties.queue.logbackrouting}")
    public String logbackrouting;

    /**
     *
     * @return
     */
    @GetMapping(value ={"/","/swagger"})
    @ApiOperation(value = "swagger首页", notes = "swagger首页")
    public String Swagger(){

        //登录成功重定向到main.html;  重定向防止表单重复提交
        return "redirect:/swagger-ui/index.html";


    }

    @PassToken //不验证 token
    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录")
    @ResponseResult//自动封装成Result
    public UserLoginResultDto Login(@RequestBody UserLoginDto user) throws Exception {
        log.info("我要登录了{}",user.toString());
        log.warn("警告日志");
        log.debug("调式日志");
        log.error("错误日志");
        UserLoginResultDto userLoginResult = baseService.Login(user);
        return  userLoginResult;
    }

    @PassToken //不验证 token
    @GetMapping("/Logqueue")
    @ApiOperation(value = "创建日志消息队列", notes = "创建日志消息队列")
    @ResponseResult//自动封装成Result
    public void Logqueue() throws Exception {
        rabbitTemplate.convertAndSend(logbackexchange,logbackrouting,"{\"app_name\":\"crmwebapi\",\"message\": \"json日志\"}");
        return ;
    }
}
